package edu.sysu.demo.rabbitmq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class Publisher {

  private static final String ROUTING_KEY_ODD = "cottle.odd";

  private static final String ROUTING_KEY_EVEN = "cottle.even";

  private RabbitTemplate rabbitTemplate;

  @Autowired
  public Publisher(RabbitTemplate rabbitTemplate) {
    this.rabbitTemplate = rabbitTemplate;
  }

  public void enqueue(Object o) {
    log.debug("Publishing message : {}", o);
    if (o.hashCode() % 2 == 0) {
      rabbitTemplate.convertAndSend(ROUTING_KEY_EVEN, o);
    } else {
      rabbitTemplate.convertAndSend(ROUTING_KEY_ODD, o);
    }
  }
}
